require(['./config'], () => {
    require(['template', 'header', 'elevateZoom', 'footer', 'fly'], (template,header) => {
      class Content {
        constructor () {
          this.moveTop()
          this.rap2Like()
          this.render().then(() => {
            if(document.cookie){
              this.fly()
            }
              this.addToCart()
          })
        }

        rap2Like(){
          $.get('http://rap2.taobao.org:38080/app/mock/265792/api/home/list', resp => {
            if(resp.code === 200){
              const { list } = resp.body
              $('#like-ul').html(template('rap2-list', { list }))
            }
          })
        }

        moveTop(){
          $(window).scroll(function () {
            if ($(window).scrollTop() >= 50) {
                $('#main-top').fadeIn();
            }
            else {
                $('#main-top').fadeOut();
            }
          })
          $('#main-top').click(function(){
            $('html,body').animate({ scrollTop: 0 }, 500);
          })
        }

        elevate(){
          $('.zoom-image').elevateZoom({
            cursor:"crosshair",
            gallery: 'showLeft-minimg'
            //zoomType: "lens",
			      //zoomType: "inner"
          })
        }

        fly(){
          $('#shopCar').on('click', function (e) {
            $('<div class="fly">').fly({
              start:{
                left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
                top: e.clientY,  //开始位置（必填）
              },
              end:{
                left: $('#flyShop').offset().left, //结束位置（必填）
                top: $('#flyShop').offset().top-$(document).scrollTop(),  //结束位置（必填）
                // width: 100, //结束时高度
                // height: 100, //结束时高度
              },
              // autoPlay: false, //是否直接运动,默认true
              speed: 1.1, //越大越快，默认1.2
              vertex_Rtop: 50, //运动轨迹最高点top值，默认20
              onEnd: function(){ 
                // 移除fly
                this.destroy()
                header.calcCartCount()
              } //结束回调
            })
          })
        }

        render(){
          return new Promise((resolve, reject) => {
            const minId = Number(location.search.slice(4))
            if(minId>=1 && minId<=56){
              $.get('/libs/json/index.json', resp => {
                if(resp){
                  $.each(resp, (index,list) => {
                    if(list.id == minId){
                      const {
                        id,
                        title,
                        price,
                        src
                      } = list
                      this.detail = {
                        id,
                        title,
                        price,
                        src
                      }
                    }
                  })
                  $('#shopshow').html(template('shopShow', {...this.detail}))
                  $('#shop-left').html(template('shop-content', {...this.detail}))
                  $('#title-i').html($('.showRight p').html())
                }
                resolve()
              })
            }else{
              const id = Number(location.search.slice(4))
              $.get('http://www.xiongmaoyouxuan.com/api/detail', { id }, resp => {
                if (resp.code === 200) {
                  const {
                    id,
                    photo,
                    title,
                    image,
                    accessoryHints,
                    price,
                    descContentList,
                    originPrice,
                    couponValue
                  } = resp.data.detail
                  this.detail = {
                    id,
                    photo,
                    title,
                    image,
                    accessoryHints,
                    price,
                    descContentList,
                    originPrice,
                    couponValue
                  }

                  $('#shopshow').html(template('shopShow-panda', { ...this.detail }))
                  $('#shop-left').html(template('shop-panda', { ...this.detail }))
                  this.elevate()
                  $('#title-i').html($('.showRight p').html())

                }
                resolve()
              })
            }
          })
        }

        addToCart() {
            $('#shopCar').on('click', () => {
              if(document.cookie){
                //this.fly()
                var num = Number($('.showRight-num input').val())
                let cart = localStorage.getItem('cart')
                if (cart) {
                  cart = JSON.parse(cart)
                  const isExist = cart.some(shop => {
                    return shop.id === this.detail.id
                  })
                  if (isExist) {
                    cart = cart.map(shop => {
                      if (shop.id === this.detail.id) shop.count += num
                      return shop
                    })
                  } else {
                    cart.push({ ...this.detail, count: num, check: true })
                  }
                  localStorage.setItem('cart', JSON.stringify(cart))
                }
                else {
                  localStorage.setItem('cart', JSON.stringify([{
                    ...this.detail,
                    count: num,
                    check: true
                  }]))
                }
              }else{
                alert('尊敬的用户,请您先登录')
              }
            })
        }

      }
      new Content()
    })
})